/*
 * Copyright (C) 2011 flashflexpro@gmail.com  All Rights Reserved.  No
 *   use, copying or distribution of this work may be made except in
 *   accordance with a valid license agreement from flashflexpro@gmail.com
 *   This notice must be included on all copies, modifications and
 *   derivatives of this work.
 *
 *   flashflexpro@gmail.com MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT
 *   THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED,
 *   INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 *   NON-INFRINGEMENT. flashflexpro@gmail.com SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED
 *   BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS
 *   SOFTWARE OR ITS DERIVATIVES.
 */

package com.flashflexpro.as3lib {
import mx.collections.IList;
import mx.core.IVisualElement;
import mx.core.IVisualElementContainer;


/**
 * No test yet!
 */
public class VisualElementContainerToListSyncer extends ModelToViewSyncer{

    public function VisualElementContainerToListSyncer(thisObj:Object, modelList:IVisualElementContainer, viewList:IList ) {
        super(thisObj, modelList, viewList);
    }

    public function get visualElementContainer():IList{
        return IList(_viewList);
    }

    public function get list():IVisualElementContainer{
        return IVisualElementContainer(_modelList);
    }

    public function syncListToVContainer( doesViewEleMatchModelEle:Function, newViewEleFromModelEle:Function ):void{

        var getModelEleAt:Function = function( index:int ):Object{
            return list.getElementAt( index );
        };

        var getViewListLength:Function = function():int{
            return visualElementContainer.length;
        };

        var getViewEleAt:Function = function( index:int ):Object{
            return visualElementContainer.getItemAt( index );
        };

        var addViewEle:Function = function( ele:IVisualElement ):void{
            visualElementContainer.addItem( ele );
        };


        var setViewEleIndex:Function = function( ele:IVisualElement, index:int ):void{
            visualElementContainer.addItemAt( visualElementContainer.removeItemAt( visualElementContainer.getItemIndex( ele ) ), index)
        };

        var removeViewEleAt:Function = function( index:int ):void{
            visualElementContainer.removeItemAt( index );
        };

        super.sync( list.numElements, getModelEleAt, getViewListLength, getViewEleAt, addViewEle, setViewEleIndex,
                removeViewEleAt, doesViewEleMatchModelEle, newViewEleFromModelEle );

    }
}
}